﻿using huqiang.Collections;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace huqiang.Communication
{
    internal struct NetworkContent:IClear
    {
        /// <summary>
        /// 内容状态
        /// </summary>
        public int State;
        /// <summary>
        /// 线程ID
        /// </summary>
        public int ThreadID;
        /// <summary>
        /// 广播消息ID
        /// </summary>
        public int Bid;
        /// <summary>
        /// 需要编码的数据
        /// </summary>
        public byte[] dat;

        public void Clear()
        {
            dat = null;
        }
    }
    internal struct RecvCache
    {
        public int Count;
        public byte[] Buffer;
        public void Add(byte[] dat, int len)
        {
            
        }
    }
    public class NetworkLink
    {
        internal LockQueueS<NetworkContent> ncStack = new LockQueueS<NetworkContent>(64);
        public int ip;
        public int port;
        public int buffIndex;
        public Int64 id;
        public IPEndPoint endPoint;
        internal bool _connect;
        public long RecyclingTime;
        protected int maxDataSize = 32768;
        /// <summary>
        /// 接收的数据长度
        /// </summary>
        internal int RecvLen;
        /// <summary>
        /// 接收到缓存数据
        /// </summary>
        internal byte[] RecvBuffer = new byte[65536];
        public bool Connected { get { return _connect; } }
        internal virtual void FreeMemory()
        {
        }
        public virtual void Dispose()
        {
        }
        /// <summary>
        /// 缓存默认大小为32，注意每帧添加的数据不要超过32
        /// </summary>
        /// <param name="id">线程ID</param>
        /// <param name="dat">如果使用广播消息，此处为空，并在第三个参数上填上广播消息的ID</param>
        /// <param name="spin">广播消息ID</param>
        /// <returns></returns>
        public virtual bool Post(byte[] dat, int bid = 0)
        {
            if (dat.Length > maxDataSize)
            {
                OutLog.WriteLine("超出允许的封包大小：" + maxDataSize);
                return false;
            }
            NetworkContent nc = new NetworkContent();
            nc.Bid = bid;
            nc.dat = dat;
            return ncStack.Enqueue(nc);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">线程ID</param>
        /// <param name="dat">如果使用广播消息，此处为空，并在第三个参数上填上广播消息的ID</param>
        /// <param name="msgID">广播消息ID</param>
        public virtual void FrocePost(int id, byte[] dat, int msgID = 0)
        {
            //var tick = DateTime.Now.Ticks;
            //while (true)
            //{
            //    if (Post(id, dat, msgID))
            //        return;
            //    if (DateTime.Now.Ticks - tick > 100000)//超过10毫秒都无法添加数据
            //    {
            //        OutLog.WriteLine("缓存已满，目标线程超过10毫秒都没有处理缓存中的任务");
            //        return;
            //    }
            //}
        }
        internal bool Get(ref NetworkContent content)
        {
            return ncStack.Dequeue(ref content);
        }
    }
}
